package com.example.unique

import android.os.Bundle
import android.util.Log
import android.view.Menu
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.navigation.NavigationView
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.drawerlayout.widget.DrawerLayout
import androidx.appcompat.app.AppCompatActivity
import com.example.unique.databinding.ActivityMainBinding
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding
    private lateinit var club:String
    private var role:Int = 0
    private lateinit var sno:String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        // 从 Intent 获取传递的数据
        val intent=getIntent()
        club = intent.getStringExtra("club").toString()
        role = intent.getIntExtra("role",2) // 默认值为 2
        sno = intent.getStringExtra("sno").toString()

        setSupportActionBar(binding.appBarMain.toolbar)

        val drawerLayout: DrawerLayout = binding.drawerLayout
        val navView: NavigationView = binding.navView
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        // 配置 AppBar 和侧边导航
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,R.id.action_chat,R.id.action_contact
            ), drawerLayout
        )
        //始终保持 ActionBar 上的按钮显示
        setupActionBarWithNavController(navController, appBarConfiguration)
       //// 侧边导航栏与 NavController 配合
        navView.setupWithNavController(navController)


        // 配置底部导航
        val bottomNavigationView: BottomNavigationView = binding.bottomNavigationView
        bottomNavigationView.setupWithNavController(navController)

        // 根据角色动态显示菜单项
        if (isAdminUser()) {
            navView.menu.findItem(R.id.nav_task_publish)?.isVisible = true
            navView.menu.findItem(R.id.nav_checkin_stats)?.isVisible = true
        } else {
            navView.menu.findItem(R.id.nav_task_publish)?.isVisible = false
            navView.menu.findItem(R.id.nav_checkin_stats)?.isVisible = false
        }



    }
    // 检查用户角色是否为管理员
    private fun isAdminUser(): Boolean {
        // 实现你的角色判断逻辑，例如从数据库或共享偏好中读取
        if(role==1){
            return true
        }
        return false
    }
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }




}